/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.commons.config;

import java.util.HashMap;
import java.util.Map;
import org.I0Itec.zkclient.ZkClient;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import cn.ac.iie.commons.config.Configuration;

/**
 *
 * @author YY
 */
public class RunTimeEnv {

    private static Configuration conf = null;
    public static final String JETTY_SERVER_IP = "jettyServerIP";
    public static final String JETTY_SERVER_PORT = "jettyServerPort";
    public static final String JETTY_SERVER_THREAD_POOL_SIZE = "jettyServerThreadPoolSize";
    public static final String METADE_CLUSTER = "metaStoreDB";
    public static final String ZK_CLUSTER = "zkCluster";
    public static ZkClient zc = null;
    public static final String LOAD_CLUSTER = "loadCluster";
    public static final String PACKAGE_SIZE_RESTRAINT = "packeSizeRestraint";
    public static final String NAME_SERVER_LIST = "nameServerList";
    public static final String RM_CC_THREAD_POOL_SIZE = "rmCCThreadPoolSize";
    public static final String MSG_CACHE_SIZE = "msgCacheSize";
    public static final String MSG_DW_NUM = "msgDispatchWorkerNum";
    public static final String METRIC_ENABLE = "metricEnable";
    public static final String METRIC_QUEUE = "metricQueue";
    public static final String METRIC_MSG_CACHE_SIZE = "metricMsgCacheSize";
    public static final String METRIC_MSG_DW_NUM = "metricMsgDispatchWorkerNum";
    public static final String DEFAULT_MQ = "defaultMQ";
    public static final String NAME_NODE = "name2Node";
    public static final String NAME_NODE_MAP = "name2NodeMap";
    public static Map<String, Object> dynamicParams = new HashMap<String, Object>();
    static Logger logger = null;

    static {
        PropertyConfigurator.configure("log4j.properties");
        logger = Logger.getLogger(RunTimeEnv.class.getName());
    }

    public static boolean initialize(Configuration pConf) {
        if (pConf == null) {
            logger.error("configuration object is null");
            return false;
        }
        conf = pConf;
        String jettyServerIP = conf.getString(JETTY_SERVER_IP, "");
        if (jettyServerIP.isEmpty()) {
            logger.error("parameter jettyServerIP does not exist or is not defined");
            return false;
        }
        dynamicParams.put(JETTY_SERVER_IP, jettyServerIP);
        try {
            int jettyServerPort = conf.getInt(JETTY_SERVER_PORT, -1);
            if (jettyServerPort < 0) {
                logger.error("parameter jettyServerPort does not exist or is not defined");
                return false;
            }
            dynamicParams.put(JETTY_SERVER_PORT, jettyServerPort);
        } catch (Exception e) {
            logger.error("parameter jettyServerPort is invalid " + e.getMessage(), e);
            return false;
        }

        try {
            int jettyServerThreadPoolSize = conf.getInt(JETTY_SERVER_THREAD_POOL_SIZE, -1);
            if (jettyServerThreadPoolSize < 0) {
                logger.error("parameter jettyServerThreadPoolSize does not exist or is not defined");
                return false;
            }
            dynamicParams.put(JETTY_SERVER_THREAD_POOL_SIZE, jettyServerThreadPoolSize);
        } catch (Exception ex) {
            logger.error("parameter jettyServerThreadPoolSize is invalid " + ex.getMessage(), ex);
            return false;
        }

        String name2Node = conf.getString(NAME_NODE, "");
        if (NAME_NODE.isEmpty()) {
            logger.error("parameter NAME_NODE does not exist or is not defined");
            return false;
        }
        dynamicParams.put(NAME_NODE, name2Node);

        Map<String, String> name2NodeMap = new HashMap<String, String>();
        String[] name2Nodes = name2Node.split("[|]");
        for (String name2NodeString : name2Nodes) {
            name2NodeMap.put(name2NodeString.split(",")[0], name2NodeString.split(",")[1]);
        }
        dynamicParams.put(NAME_NODE_MAP, name2NodeMap);

        return true;
    }

    public static void dumpEnvironment() {
        conf.dumpConfiguration();
    }

    public static void addParam(String pParamName, Object pValue) {
        synchronized (dynamicParams) {
            dynamicParams.put(pParamName, pValue);
        }
    }

    public static Object getParam(String pParamName) {
        return dynamicParams.get(pParamName);
    }
}
